package com.capri.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.capri.R;
import com.capri.activity.OrderDetailActivity;
import com.capri.model.Action.CallAction;
import com.capri.model.Action.OrderStatusAction;
import com.capri.model.bean.TodayOrderEntity;
import com.capri.utils.TimeUtils;
import com.capri.widget.DashBoard;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

public class SimpleServiceInfoAdapter extends RecyclerView.Adapter<SimpleServiceInfoAdapter.ViewHolder> {

    private List<TodayOrderEntity> datas = new ArrayList<>();
    private Context con;

    public SimpleServiceInfoAdapter(Context context) {
        this.con = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_simple_service_info, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String serviceStatus = datas.get(position).getServiceStatus();
        holder.dash_board_time.setPercentUser(0);
        holder.dash_board_time.setServiceNameUser(datas.get(position).getOrderName());
        holder.dash_board_time.setServiceTimeTextUser("_ _:_ _");
        holder.layout_root_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(con, OrderDetailActivity.class);
                intent.putExtra("key_id", datas.get(position).getOrderId());
                con.startActivity(intent);
            }
        });
        if ("waitConfirm".equals(serviceStatus)) {
            serviceStatus = "待接单";//待接单
            holder.layout_simple_info_action.setVisibility(View.GONE);
            holder.tv_confirm_order.setVisibility(View.VISIBLE);
            holder.tv_confirm_order.setText("确定订单");
            holder.tv_confirm_order.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    OrderStatusAction action = new OrderStatusAction();
                    action.setStatus("waitStartOff");
                    action.setOrderId(datas.get(position).getOrderId());
                    EventBus.getDefault().post(action);
                }
            });
            holder.tv_contact.setOnClickListener(null);
            holder.tv_confirm_arrived.setOnClickListener(null);
        } else if ("waitStartOff".equals(serviceStatus)) {
            serviceStatus = "待出诊";//待出诊
            holder.layout_simple_info_action.setVisibility(View.VISIBLE);
            holder.tv_confirm_order.setVisibility(View.GONE);
            holder.tv_confirm_arrived.setText("确定出发");
            holder.tv_confirm_order.setOnClickListener(null);
            holder.tv_contact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CallAction event = new CallAction();
                    event.setPhoneNum(datas.get(position).getPatientPhone());
                    EventBus.getDefault().post(event);
                }
            });
            holder.tv_confirm_arrived.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    OrderStatusAction action = new OrderStatusAction();
                    action.setOrderId(datas.get(position).getOrderId());
                    action.setStatus("startOff");
                    EventBus.getDefault().post(action);
                }
            });
        } else if ("startOff".equals(serviceStatus)) {
            serviceStatus = "已出诊";//已出诊
            holder.layout_simple_info_action.setVisibility(View.VISIBLE);
            holder.tv_confirm_order.setVisibility(View.GONE);
            holder.tv_confirm_arrived.setText("确定到达");
            holder.tv_confirm_order.setOnClickListener(null);
            holder.tv_contact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CallAction event = new CallAction();
                    event.setPhoneNum(datas.get(position).getPatientPhone());
                    EventBus.getDefault().post(event);
                }
            });
            holder.tv_confirm_arrived.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    OrderStatusAction action = new OrderStatusAction();
                    action.setOrderId(datas.get(position).getOrderId());
                    action.setStatus("arrived");
                    EventBus.getDefault().post(action);
                }
            });
        } else if ("arrived".equals(serviceStatus)) {
            serviceStatus = "已到达";//已到达
            holder.layout_simple_info_action.setVisibility(View.GONE);
            holder.tv_confirm_order.setVisibility(View.VISIBLE);
            holder.tv_confirm_order.setText("开始服务");
            holder.tv_contact.setOnClickListener(null);
            holder.tv_confirm_arrived.setOnClickListener(null);
            holder.tv_confirm_order.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    OrderStatusAction action = new OrderStatusAction();
                    action.setOrderId(datas.get(position).getOrderId());
                    action.setStatus("startService");
                    EventBus.getDefault().post(action);
                }
            });
        } else if ("startService".equals(serviceStatus)) {
            serviceStatus = "服务中";//已到达
            holder.layout_simple_info_action.setVisibility(View.GONE);
            holder.tv_confirm_order.setVisibility(View.VISIBLE);
            holder.tv_confirm_order.setText("结束服务");
            holder.tv_contact.setOnClickListener(null);
            holder.tv_confirm_arrived.setOnClickListener(null);
            holder.tv_confirm_order.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    OrderStatusAction action = new OrderStatusAction();
                    action.setOrderId(datas.get(position).getOrderId());
                    action.setStatus("serviceFinish");
                    EventBus.getDefault().post(action);
                }
            });
            holder.dash_board_time.setPercentUser(0);
            holder.dash_board_time.setServiceNameUser(datas.get(position).getOrderName());
            //2019-12-02 20:09:09
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            try {
                Date serviceDate = format.parse(datas.get(position).getServiceDate());
                Date date = new Date();
                long time = (date.getTime() - serviceDate.getTime());
                long time2 = datas.get(position).getServingDuration() * 60 * 1000 - time;
                String formatTime = TimeUtils.formatDateTime(time2);
                if (time2 <= 0) {
                    time2 = 0;
                    formatTime = "00:00";
                }
                Logger.i("formatTime-->" + formatTime);
                holder.dash_board_time.setServiceTimeTextUser(formatTime);
                float p = (((float) time2) / (datas.get(position).getServingDuration() * 60 * 1000));
//                float p = (((float) time) / (datas.get(position).getServingDuration() * 60 * 1000));
                if (p > 1) {
                    p = 1;
                }
                holder.dash_board_time.setPercentUser(p);
            } catch (ParseException e) {
                e.printStackTrace();
            }
//            holder.dash_board_time.setServiceTimeTextUser("_ _:_ _");
        }
        holder.tv_address.setText(datas.get(position).getServiceAddress());
        holder.tv_contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CallAction event = new CallAction();
                event.setPhoneNum(datas.get(position).getPatientPhone());
                EventBus.getDefault().post(event);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        DashBoard dash_board_time;
        TextView tv_address;
        TextView tv_contact;
        TextView tv_confirm_arrived;
        TextView tv_confirm_order;
        LinearLayout layout_simple_info_action;
        RelativeLayout layout_root_view;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            dash_board_time = itemView.findViewById(R.id.dash_board_time);
            tv_address = itemView.findViewById(R.id.tv_address);
            tv_contact = itemView.findViewById(R.id.tv_contact);
            tv_confirm_arrived = itemView.findViewById(R.id.tv_confirm_arrived);
            tv_confirm_order = itemView.findViewById(R.id.tv_confirm_order);
            layout_simple_info_action = itemView.findViewById(R.id.layout_simple_info_action);
            layout_root_view = itemView.findViewById(R.id.layout_root_view);
        }
    }

    public void setDatas(List<TodayOrderEntity> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            notifyDataSetChanged();
        }
    }
}
